package com.example.fw;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

public class ContactHelper extends HelperWithWebDriverBase{
	
 	private Set<ContactObjects> contactCache;
	private Contacts contactModel;

	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	public Contacts getContacts() {
		if (contactModel == null) {
			contactModel = manager.getHibernateHelper().getContacts();
		}
		return contactModel;
	}
	
	public Contacts getUiContacts() {
		HashSet<ContactObjects> set = new HashSet<ContactObjects>();
		manager.getNavigationHelper().openMainPage();
		List<WebElement> checkboxes = findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			Pattern p = Pattern.compile(".*\\((.*)\\)");
			Matcher m = p.matcher(title);
			if (m.matches()) {
				title = m.group(1);			
			}
			ContactObjects contact = new ContactObjects()
				.setFullname(title)
				.setId(checkbox.getAttribute("value"));
			set.add(contact);
		}
		return new Contacts(set);
	}
	
	public void createContact(ContactObjects fullContact) {
		manager.getNavigationHelper().goToAddNewContactPage();
		fillContactForm(fullContact, true);
		submitCreation();
		returnToHomePage();
		contactModel = contactModel.withAdded(fullContact);
	}
	
	public void modifyContact(ContactObjects fullContact) {
		manager.getNavigationHelper().openMainPage();
		initFirstContactModification();
		fillContactForm(fullContact, false);
		submitModification();
		returnToHomePage();
		contactModel = contactModel.without(fullContact);
	}

 	public void modifyRandomContact(String idContact, ContactObjects fullContact) {
 		manager.getNavigationHelper().openMainPage();
 		initRandomContactModification(idContact);
 		fillContactForm(fullContact, false);
 		submitModification();
  		returnToHomePage();
  		contactModel = contactModel.without(idContact);
 	}

	public void removeFirstContact() {
		manager.getNavigationHelper().openMainPage();
		initFirstContactModification();
		submitDeletion();
		returnToHomePage();
		contactCache = null;
	}

	//fills page
	private void fillContactForm(ContactObjects groupObject, boolean fillGroup) {
		type(By.name("firstname"), groupObject.firstname);
		type(By.name("lastname"), groupObject.lastname);
		type(By.name("address"), groupObject.address);
		type(By.name("home"), groupObject.home);
		type(By.name("mobile"), groupObject.mobile);
		type(By.name("work"), groupObject.work);
		type(By.name("email"), groupObject.email);
		type(By.name("email2"), groupObject.email2);
		
		select(By.name("bday"), groupObject.bday);

		GregorianCalendar cal = new GregorianCalendar();
		cal.set(Integer.parseInt(groupObject.byear), Integer.parseInt(groupObject.bmonth), Integer.parseInt(groupObject.bday));
			
		String month = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US);
		select(By.name("bmonth"), month);
		
		type(By.name("byear"), groupObject.byear);
		
		selectByIndex(By.name("new_group"), Integer.parseInt(groupObject.group));
    				
		type(By.name("address2"), groupObject.address2);
		type(By.name("phone2"), groupObject.phone2);
	}
	
	private void initFirstContactModification() {
		click(By.xpath("(//img[@alt='Edit'])[1]"));
	}

	private void initRandomContactModification(String idContact) {
		//click(By.xpath("(//img[@alt='Edit'])[1]"));
		WebElement checkbox =  findElement(By.xpath("//input[@value="+idContact+"]"));
		checkbox.click();
	}

	private void submitDeletion() {
		click(By.xpath("(//input[@name='update'])[2]"));
	}

	private void returnToHomePage() {
		click(By.linkText("home page"));
	}
}